package com.chat.api.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Component
public class CheckSumInterceptor implements HandlerInterceptor {

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
		HttpSession session = request.getSession();
		String user = (String) session.getAttribute(session.getId());
		String url = request.getRequestURI();
		if (url.equals("/templates/login.html")) return true;
		if(StringUtils.isBlank(user)){
			response.sendRedirect("/templates/login.html");
			return false;
		}
		return true;
	}
}
